import { Client } from "./mod.ts";
import { User } from "./user.ts";
import { Permission } from "./repo.ts";

/**
 * Collaborator represents a collaborator of a repository.
 */
export interface Collaborator {
  // Extends User
  id: number;
  username: string;
  login: string;
  full_name: string;
  email: string;
  avatar_url: string;
  // Additional fields
  permissions: Permission;
}

/**
 * Options for adding a collaborator
 */
export interface AddCollaboratorOption {
  permission?: string;
}

/**
 * Repository Collaborator API endpoints
 */
export class CollaboratorAPI {
  constructor(private client: Client) {}

  /**
   * List collaborators of a repository
   */
  async listCollaborators(owner: string, repo: string): Promise<Collaborator[]> {
    return await this.client.doRequest<Collaborator[]>(
      "GET", 
      `/repos/${owner}/${repo}/collaborators`
    );
  }

  /**
   * Add a collaborator to a repository
   */
  async addCollaborator(owner: string, repo: string, collaborator: string, options: AddCollaboratorOption = {}): Promise<void> {
    await this.client.doRequest<void>(
      "PUT", 
      `/repos/${owner}/${repo}/collaborators/${collaborator}`, 
      { "Content-Type": "application/json" },
      options
    );
  }

  /**
   * Delete a collaborator from a repository
   */
  async deleteCollaborator(owner: string, repo: string, collaborator: string): Promise<void> {
    await this.client.doRequest<void>(
      "DELETE", 
      `/repos/${owner}/${repo}/collaborators/${collaborator}`
    );
  }

  /**
   * Check if a user is a collaborator of a repository
   */
  async isCollaborator(owner: string, repo: string, collaborator: string): Promise<boolean> {
    try {
      await this.client.doRequest<void>(
        "GET", 
        `/repos/${owner}/${repo}/collaborators/${collaborator}`
      );
      return true;
    } catch (error) {
      return false;
    }
  }
} 